﻿using HY.DataCore.ExtensionAttribute;
using HY.DataCore.Migrations;
using HY.Entity;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace HY.DataCore
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
    public class EntityContext : DbContext
    {
        public EntityContext() : base("ConnString")
        {
            GenerateConfiguration();
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="nameOrConnectionString">数据库名称或连接字符串。</param>
        public EntityContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
            GenerateConfiguration();
        }
        /// <summary>
        /// 构造函数，用于EF和Dapper共用一个连接对象
        /// </summary>
        /// <param name="dbConnecttion">数据库连接对象</param>
        public EntityContext(DbConnection dbConnection) : base(dbConnection, true)
        {
            GenerateConfiguration();
        }

        private void GenerateConfiguration()
        {
            //自动迁移数据库为最新版本，如果放在此处，每次访问数据库，都会调用Configuration进行比对，因此放到UnitOfWork中，在程序加载时验证一次即可
            //Database.SetInitializer(new MigrateDatabaseToLatestVersion<EntityContext, Configuration>());
        }

        public DbSet<SysUser> SysUser { get; set; }
        public DbSet<SysRole> SysRole { get; set; }
        public DbSet<SysDept> SysDept { get; set; }
        public DbSet<SysLog> SysLog { get; set; }
        public DbSet<SysModule> SysModule { get; set; }
        public DbSet<SysRoleModule> SysRoleModule { get; set; }
        public DbSet<Park> Park { get; set; }
        public DbSet<Storehouse> Storehouse { get; set; }
        public DbSet<Location> Location { get; set; }
        public DbSet<StoreType> StoreType { get; set; }
        public DbSet<MonitorData> MonitorData { get; set; }
        public DbSet<RentDetail> RentDetail { get; set; }
        public DbSet<LocationImage> LocationImage { get; set; }
        public DbSet<GPSData> GPSData { get; set; }
        public DbSet<Region> Region { get; set; }
        public DbSet<Equipment> Equipment { get; set; }
        public DbSet<EquipmentType> EquipmentType { get; set; }
        public DbSet<EquipmentBind> EquipmentBind { get; set; }

        public DbSet<Stock> Stock { get; set; }
        public DbSet<StockIn> StockIn { get; set; }
        public DbSet<StockOut> StockOut { get; set; }
        public DbSet<StockInfo> StockInfo { get; set; }
        public DbSet<NeedInfo> NeedInfo { get; set; }

        public DbSet<ResourceDict> ResourceDict { get; set; }


        public DbSet<RentRecord> RentRecord { get; set; }
        public DbSet<RentRecord2> RentRecord2 { get; set; }

        public DbSet<LocationHistory> LocationHistory { get; set; }

        public DbSet<Advert> Advert { get; set; }
        public DbSet<Notice> Notice { get; set; }

        /// <summary>
        /// 模型配置重写
        /// </summary>
        /// <param name="modelBuilder">数据实体生成器</param>
        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {

            // 禁用实体状态改变跟踪
            Configuration.AutoDetectChangesEnabled = false;
            // 禁用导航属性延迟加载
            Configuration.LazyLoadingEnabled = false;
            // 禁用自动创建代理类
            Configuration.ProxyCreationEnabled = false;
            // 禁用数据库null语义
            Configuration.UseDatabaseNullSemantics = true;
            // 禁用保存时验证所跟踪实体
            Configuration.ValidateOnSaveEnabled = false;

            // 禁用一对多级联删除
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            // 禁用多对多级联删除
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
            // 禁用表名自动复数规则
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            //自定义规则
            //Decimal精度设置
            modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());

            base.OnModelCreating(modelBuilder);
        }
    }
}
